﻿using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;

namespace Sok.Munkae.Util
{
    public class SQLHelper
    {
        private string strConnectionString = string.Empty;

        public SqlConnection connection()
        {
            var con =
                new SqlConnection(
                    ConfigurationManager.ConnectionStrings["SokMunkaeDemoConnectionString"].ConnectionString);
            con.Open();
            return con;
        }

        public SqlDataReader ExecuteReader(string query, Dictionary<string, object> commandParameters)
        {
            using (SqlConnection con = connection())
            {
                var cmd = new SqlCommand(query, con);
                foreach (var p in commandParameters)
                {
                    cmd.Parameters.Add(p.Key, p.Value);
                }
                SqlDataReader dr = cmd.ExecuteReader();
                return dr;
            }
        }

        public object ExecuteScalar(string query, Dictionary<string, object> commandParameters)
        {
            using (SqlConnection con = connection())
            {
                var cmd = new SqlCommand(query, con);
                foreach (var p in commandParameters)
                {
                    cmd.Parameters.Add(p.Key, p.Value);
                }
                object retVal = cmd.ExecuteScalar();
                return retVal;
            }
        }
    }
}